
import json

# 配置文件路径
CONFIG_FILE = "config.json"
filesconfig_FILE ="filesconfig.json"
# 默认配置
default_config = {
    "bootflag":1,
    "itmes":[True,True,True,True,True,True,True,True,True,True,True,True,True],
    "wifi": {
        "ssid": "TP-LINK_9B6E",
        "password": "20140401"
    },
    "timerThreshold": 1,
    "PhaseAngleThreshold":30,
    "Voltagethreshold":172,
    "Currentthreshold":0
}
# 默认配置
default_filesconfig = {
    "filesname":[
                "main.py",           
                "beep.py",
                "ble.py",
                "display.py",
                "em_485.py",
                "font.py",
                "gb2312.py",
                "GPIO.py",
                "input_chinese.py",
                "kalman.py",
                "loginfo.py",
                "OLED.py",
                "sh1106.py",
                "ssd1306.py",
                "switch.py"
                ]     
}

def read_config():
    try:
        with open(CONFIG_FILE, 'r') as f:
            config = json.load(f)
            print("配置文件读取成功")
            return config
    except:
        print("配置文件不存在或格式错误，使用默认配置")
        # 写入默认配置
        write_config(default_config)
        return default_config

def write_config(config):
    try:
        with open(CONFIG_FILE, 'w') as f:
            json.dump(config, f)
        print("配置文件写入成功")
        return True
    except Exception as e:
        print("配置文件写入失败:", e)
        return False


def read_filesconfig():
    try:
        with open(filesconfig_FILE, 'r') as f:
            filesconfig = json.load(f)
            print("配置文件读取成功")
            return filesconfig
    except:
        print("配置文件不存在或格式错误，使用默认配置")
        # 写入默认配置
        write_filesconfig(default_filesconfig)
        return default_filesconfig

def write_filesconfig(filesconfig):
    try:
        with open(filesconfig_FILE, 'w') as f:
            json.dump(filesconfig, f)
        print("配置文件写入成功")
        return True
    except Exception as e:
        print("配置文件写入失败:", e)
        return False